package com.tianruan.psychological.moduls.sys.user.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianruan.psychological.common.utils.encryption.Md5Utils;
import com.tianruan.psychological.moduls.sys.user.entry.db.UserDO;
import com.tianruan.psychological.moduls.sys.user.entry.dto.UserDTO;
import com.tianruan.psychological.moduls.sys.user.entry.vo.UserVO;
import com.tianruan.psychological.moduls.sys.user.mapper.UserMapper;
import com.tianruan.psychological.moduls.sys.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Dong
 * @Email: dong980514280@gmail.com
 * @Create: 2020-09-20 10:07
 * @PS
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements IUserService {


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleServiceImpl userRoleService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    public UserDTO getUserPermissionByNo(String username) {
        return userMapper.getUserPermissionByNo(username);
    }

    public IPage<UserVO> pageUser(Page<UserVO> page,
                                  String no,
                                  String username,
                                  String phone,
                                  String email,
                                  String role) {
        return userMapper.pageUser(page, no, username, phone, email, role);
    }

    public UserVO getUserRoleById(String id) {
        return userMapper.getUserRoleById(id);
    }

    @Transactional
    public UserDTO updateUser(String id, UserDTO userDTO){
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userDTO,userDO);
        userDO.setId(id);
        userMapper.updateById(userDO);
        userRoleService.insertUserRole(id, userDTO.getRoleIds());
        return userDTO;
    }


    @Transactional
    public UserDTO insertUser(UserDTO userDTO) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userDTO, userDO);
        // 设置密码，暂时以自己工号为密码
        String passoword = passwordEncoder.encode(userDO.getNo());
        userDO.setPassword(passoword);
        userMapper.insert(userDO);
        userRoleService.insertUserRole(userDO.getId(), userDTO.getRoleIds());
        return userDTO;
    }

    public String getPassword(String id) {
        return userMapper.getPassword(id);
    }

    /**
     * 判断密码是否相等
     * @param id
     * @param password
     * @return
     */
    public Boolean equalsPassword(String id, String password) {
        String pw = this.getPassword(id);
        return Md5Utils.validate(password, pw);
    }

    /**
     * 根据id修改密码
     * @param id
     * @param password
     */
    public void modifyPassword(String id, String password) {
        password = Md5Utils.entrypt(password);
        UserDO userDO = new UserDO().setPassword(password);
        userDO.setId(id);
        userMapper.updateById(userDO);
    }
}
